#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "log.hpp"
#include "Common.hpp"
#include "Protocol.hpp"

using namespace LogModule;

//./client 127.0.0.1 8080
int main(int argc, char *argv[])
{
    ENABLE_CONSOLE_LOG();

    if (argc != 3)
    {
        std::cout << "Uasge:" << argv[0] << " ip port" << std::endl;
        return 1;
    }
    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    int sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        LOG(LogLevel::ERROR) << "create socket failed";
        return 2;
    }

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(server_port);
    server_addr.sin_addr.s_addr = inet_addr(server_ip.c_str());

    int n = ::connect(sockfd, CONV(&server_addr), sizeof(server_addr));
    if (n < 0)
    {
        LOG(LogLevel::ERROR) << "connect failed";
        return 3;
    }

    while(true)
    {
        int x, y;
        char oper;
        std::cout << "please input x ";
        std::cin >> x;
        std::cout << "please input y ";
        std::cin >> y;
        std::cout << "please input oper ";
        std::cin >> oper;
        Request reques(x, y, oper);
        std::string message;
        // 1.序列化
        if (!reques.Serialize(message))
        {
            std::cout << "序列化 失败" << std::endl;
        }
        // 2.Encode
        if (!Encode(message))
        {
            std::cout << "Encode 失败" << std::endl;
        }
        // 3. 发送
        int n2 = ::send(sockfd, message.c_str(), message.size(), 0);
        if (n2 > 0)
        {
            char inbuffer[1024];
            int m = ::recv(sockfd, inbuffer, sizeof(inbuffer)-1, 0);//目前还没有保证报文的完整性
            if (m > 0)
            {
                inbuffer[m] = 0;
                std::string package = inbuffer;
                std::string content;

                // 4. 读到应答完整--暂定, decode
                if(!Decode(package, &content))
                {
                    std::cout<<"Decode error"<<std::endl;
                    break;
                }


                //5.反序列化
                Response resp;
                if(!resp.Deserialize(content))
                {
                    std::cout<<"Deserialize error"<<std::endl;
                    break;
                }
                // 6. 得到结构化数据
                std::cout << resp.Result() << "[" << resp.Code() << "]" << std::endl;
                
            }
            else
            {
                break;
            }
        }
        else
            break;
    }

    ::close(sockfd);
return 0;
}


// //Echo Client
// std::string message;
// while(true)
// {
//     char inbuffer[1024];
//     std::cout<<"input message:";
//     std::getline(std::cin ,message);

//     //发给服务器
//     int n2 = write(sockfd, message.c_str() , message.size());
//     if(n2 > 0)
//     {
//         int m = read(sockfd, inbuffer ,sizeof(inbuffer)-1);
//         if(m >0)
//         {
//             inbuffer[m] = 0;
//             std::cout<<inbuffer<<std::endl;

//         }
//         else
//         {
//             break;
//         }
//     }
//     else
//         break;
// }

